package com.cs410.collabwriting.main;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.cs410.collabwriting.tools.DatabaseManager;
import com.cs410.collabwriting.tools.queryTools.DatabaseTable;
import com.cs410.collabwriting.tools.queryTools.QueryBuilder;
import com.cs410.collabwriting.tools.queryTools.QueryCondition;
import com.cs410.collabwriting.tools.queryTools.QueryType;
import com.cs410.collabwriting.tools.queryTools.tables.COMMENT_TABLE;

// how are comments actually created. and where.
public class Comment implements DatabaseObjects {

	private int m_commentID = -1;
	private int m_storyID = -1;
	private String m_userID = null;
	private String m_comment = null;
	private String m_timeStamp = null;
	
	public Comment() {
		
	}
	
	public Comment( int commentID ) {
		m_commentID = commentID;
		ArrayList<String[]> results = DatabaseManager.executeQueryWithColumnNames(
			new QueryBuilder(QueryType.SELECTION)
			.addTables(DatabaseTable.COMMENT_TABLE)
			.addConditions(QueryCondition.AND, COMMENT_TABLE.COMMENTID, QueryCondition.EQ, commentID)
			.value() );
		if( results != null && results.size() > 1 ) {
			m_storyID = Integer.parseInt(DatabaseManager.getFirstValueInColumn( results, COMMENT_TABLE.STORYID ).trim() );
			m_userID = DatabaseManager.getFirstValueInColumn( results, COMMENT_TABLE.USERID );
			m_comment = DatabaseManager.getFirstValueInColumn( results, COMMENT_TABLE.COMMENT );
			m_timeStamp = DatabaseManager.getFirstValueInColumn( results, COMMENT_TABLE.TIMESTAMP );
		}
	}

	public int getCommentID() {
		return m_commentID;
	}
	
	public int getStoryID() {
		return m_storyID;
	}
	
	public String getUserID() {
		return m_userID;
	}
	
	public String getTimeStamp() {
		if( m_timeStamp == null && m_commentID > 0 ) {
			m_timeStamp = DatabaseManager.executeExactQuery(
				new QueryBuilder(QueryType.SELECTION)
				.addColumns(COMMENT_TABLE.TIMESTAMP)
				.addTables(DatabaseTable.COMMENT_TABLE)
				.addConditions(QueryCondition.AND, COMMENT_TABLE.COMMENTID, QueryCondition.EQ, m_commentID )
				.value() );
		}

		if( m_timeStamp == null ) {
			m_timeStamp = new Date().toString();
		}
		
        SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
        
        try {
            Date date = parser.parse(m_timeStamp);
            return formatter.format(date);
        } catch (ParseException e) {            
            return m_timeStamp.substring(0, m_timeStamp.length()-2);
        }  
	}
	
	public String getComment() {
		return m_comment;
	}


	public void update() {}
	
	public String getTimeStampinNum(){
		if( m_timeStamp == null) {
			m_timeStamp = DatabaseManager.executeExactQuery(
				new QueryBuilder(QueryType.SELECTION)
				.addColumns(COMMENT_TABLE.TIMESTAMP)
				.addTables(DatabaseTable.COMMENT_TABLE)
				.addConditions(QueryCondition.AND, COMMENT_TABLE.COMMENTID, QueryCondition.EQ, m_commentID)
				.value() );
		}
		
        return m_timeStamp.substring(0, m_timeStamp.length()-2);
	}
}
